package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.pojo.TbSpec;
import com.guigu.qingcheng.pojo.vo.ParaVo;
import com.guigu.qingcheng.service.TbSpecService;
import com.guigu.qingcheng.utils.PageUtil;
import com.guigu.qingcheng.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
*
* @author Jiang
* @date 2020/07/27
*/
@Slf4j
@RestController
@RequestMapping("/spec")
@Api(tags = "TbSpecController")
public class TbSpecController {
    @Resource
    private TbSpecService tbSpecService;

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") Integer id) {

        TbSpec tbSpec = tbSpecService.findById(id);
        return R.ok().data("tbSpec",tbSpec);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public R list() {
        List<TbSpec>  list = tbSpecService.queryAll();
        return R.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages/{pageNum}/{pageSize}")
    public R listPages(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageUtil<TbSpec> pageUtils = tbSpecService.findListAll(pageNum, pageSize);

        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("/pageQueryByAll/{pageNum}/{pageSize}")
    public R pageQueryByAll(@RequestBody TbSpec tbSpec, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageUtil<TbSpec> pageUtils = tbSpecService.pageQueryByAll(pageNum, pageSize, tbSpec);
        return R.ok().data("pageUtils",pageUtils);
    }


    @ApiOperation(value = "获取信息（list分页）带条件 连表查")
    @PostMapping("/specPageQueryByAll/{pageNum}/{pageSize}")
    public R specPageQueryByAll(@RequestBody TbSpec tbSpec, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageUtil<ParaVo> pageUtils = tbSpecService.specPageQueryByAll(pageNum, pageSize, tbSpec);
        return R.ok().data("pageUtils",pageUtils);
    }



    @ApiOperation(value = "添加信息")
    @PostMapping("/add")
    public R add(@RequestBody TbSpec tbSpec) {

        boolean b = tbSpecService.saveTbSpec(tbSpec);
        if(b){
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public R update(@RequestBody TbSpec tbSpec) {
        boolean b = tbSpecService.updateTbSpec(tbSpec);

        if(b){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id) {
        boolean b = tbSpecService.deleteById(id);
        if(b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation(value = "通过模板Id查询规格列表")
    @GetMapping("/selectSpecByTemplateId/{templateId}")
    public R selectSpecByTemplateId(@PathVariable Integer templateId){
        List<TbSpec> tbSpecs = tbSpecService.selectSpecByTemplateId(templateId);
        return R.ok().data("specs", tbSpecs);
    }

    @ApiOperation(value = "根据分类名字查询规格参数")
    @PostMapping("/queryListByCategoryName")
    public R queryListByCategoryName(String name){
        List<Map> specList = tbSpecService.queryListByCategoryName(name);
        return R.ok().data("specList", specList);
    }
}
